/**
 * @filename:SysUserRoleController 2025-05-27
 * @project starhub-schema  V1.0
 */
package com.starhub.engine.sys.role.controller;

import com.starhub.common.bean.ID;
import com.starhub.engine.sys.role.entity.SysUserRole;
import com.starhub.engine.sys.role.request.SysUserRoleBsRequest;
import com.starhub.engine.sys.role.service.SysUserRoleService;
import com.starhub.common.bean.ResultResp;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.utils.Util;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;
/**   
 * <p>说明： 用户角色关联表设置API接口层</P>
 * @version: V1.0
 * @author: luosw
 * @time    2025-05-27
 *
 */
@Api(value="用户角色关联表设置后台接口", tags = {"用户角色关联表设置后台接口"},description = "编写者:luosw" )
@RestController
@RequestMapping("/engine/starhub/userRole")
public class SysUserRoleConfigController {

    @Autowired
    private SysUserRoleService sysUserRoleService;
    /**
    * 日志
    */
    private static final Logger log = LoggerFactory.getLogger(SysUserRoleConfigController.class);

    /**
    * 用户角色关联表设置新增
    */
    @ApiOperation(value = "用户角色关联表设置新增", notes = "用户角色关联表设置新增")
    @PostMapping("/add")
    public ResultResp add(@RequestBody SysUserRoleBsRequest request){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            ResultResp = ResultResp.success(sysUserRoleService.add(user, request).toString());
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
    * 用户角色关联表设置修改
    */
    @ApiOperation(value = "用户角色关联表设置修改", notes = "用户角色关联表设置修改")
    @PostMapping("/update")
    public ResultResp update(@RequestBody SysUserRole userRole){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            ResultResp = ResultResp.success(sysUserRoleService.update(user, userRole));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
    * 用户角色关联表设置信息获取
    */
    @ApiOperation(value = "用户角色关联表设置信息获取", notes = "用户角色关联表设置信息获取")
    @GetMapping("/getInfo/{id}")
    public ResultResp getInfo(@PathVariable Long id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            ResultResp = ResultResp.success(sysUserRoleService.getInfo(user, id));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
    * 用户角色关联表设置逻辑删除
    */
    @ApiOperation(value = "用户角色关联表设置逻辑删除", notes = "用户角色关联表设置逻辑删除")
    @PostMapping("/delete")
    public ResultResp deleteByType(@RequestBody ID id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            sysUserRoleService.deleteByType(user, id.getIds(), 1);
            ResultResp = ResultResp.success("删除成功");
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }


    /**
    * 用户角色关联表设置物理删除
    */
    @ApiOperation(value = "用户角色关联表设置物理删除", notes = "用户角色关联表设置物理删除")
    @PostMapping("/deleteRecord")
    public ResultResp deleteRecord(@RequestBody ID id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            sysUserRoleService.deleteRecord(user, id.getIds());
            ResultResp = ResultResp.success("删除成功");
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
    * 用户角色关联表设置列表获取
    */
    @ApiOperation(value = "用户角色关联表设置列表获取", notes = "用户角色关联表设置列表获取")
    @GetMapping("/getList")
    public ResultResp getList(HttpServletRequest request){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Map<String , Object> params = Util.request2Map(request);
            ResultResp = ResultResp.success( sysUserRoleService.getList(user, params));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

}